package com.wokeMy.view.channe.rongbangjf;

import android.app.Dialog;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.woke.MainActivity;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.woke.data.Data_bindcarlist;
import com.woke.method.MyApp;
import com.woke.serizedatas.Datas_load;
import com.wokeMy.view.base.BaseActivity;
import com.wokeMy.view.model.Constant;
import com.wokeMy.view.util.Util;
import com.zhongcai.online.R;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;

public class RbqdzfActivity extends BaseActivity implements View.OnClickListener{

    Data_bindcarlist paylistdata;
    Intent intent;
    float money;
    TextView fk_carno_double,fk_shoucarno_double,fk_carname_double,fk_idcarno_double,money_double,cvv_double,yxq_double,phone_double,hqyzm_double_tv;
    EditText yzm_double_et;
    String user_id,kspayorderid,yzm,orderno,jscardno;
    int way_id;
    LinearLayout yxq_double_ll,cvv_double_ll;
    View line_double_view,linetwo_double_view;
    ImageView back_double_iv;
    Button czhi_double_bt;
    Dialog dialog;
    RelativeLayout yzm_double_rl;
    @Override
    public void setContentView() {
        setContentView(R.layout.activity_html_double);
    }

    @Override
    public void initView() {
        fk_shoucarno_double=myFindViewById(R.id.fk_shoucarno_double);
        fk_carno_double=myFindViewById(R.id.fk_carno_double);
        fk_carname_double=myFindViewById(R.id.fk_carname_double);
        fk_idcarno_double=myFindViewById(R.id.fk_idcarno_double);
        money_double=myFindViewById(R.id.money_double);
        cvv_double=myFindViewById(R.id.cvv_double);
        yxq_double=myFindViewById(R.id.yxq_double);
        phone_double=myFindViewById(R.id.phone_double);
        yzm_double_et=myFindViewById(R.id.yzm_double_et);
        hqyzm_double_tv=myFindViewById(R.id.hqyzm_double_tv);
        yxq_double_ll = myFindViewById(R.id.yxq_double_ll);
        cvv_double_ll = myFindViewById(R.id.cvv_double_ll);
        line_double_view = myFindViewById(R.id.line_double_view);
        linetwo_double_view = myFindViewById(R.id.linetwo_double_view);
        back_double_iv = myFindViewById(R.id.back_double_iv);
        czhi_double_bt = myFindViewById(R.id.czhi_double_bt);
        yzm_double_rl = myFindViewById(R.id.yzm_double_rl);
        back_double_iv.setOnClickListener(this);
        czhi_double_bt.setOnClickListener(this);
    }

    @Override
    public void initData() {
        MyApp myApp=(MyApp)getApplication();
        Datas_load datas_load=myApp.getDatas_load();
        user_id=datas_load.getId();
        intent=getIntent();
        paylistdata=(Data_bindcarlist)intent.getSerializableExtra("paylistdata");
        jscardno=intent.getStringExtra("jscardno");
        money = intent.getFloatExtra ("money", 0.01f);
        way_id = intent.getIntExtra ("way_id", 0);
        orderno = intent.getStringExtra("orderno");
        Log.e("initData2",money+"////"+way_id);
    }

    @Override
    public void initActivity() {
        hqyzm_double_tv.setVisibility(View.GONE);
        String shou_card_no =jscardno;
        fk_shoucarno_double.setText(shou_card_no.substring(0, 4)+"    "+"***"+"    "+"***"+"    "+"***"+"    "+shou_card_no.substring(shou_card_no.length()-3,shou_card_no.length()));

        if (paylistdata.type.equals("0")){
            //信用卡
            yxq_double_ll.setVisibility(View.VISIBLE);
            cvv_double_ll.setVisibility(View.VISIBLE);
            line_double_view.setVisibility(View.VISIBLE);
            linetwo_double_view.setVisibility(View.VISIBLE);
            cvv_double.setText(paylistdata.code);
            yxq_double.setText(paylistdata.validthru);
        }
        String pay_card_no =paylistdata.cardno;
        fk_carno_double.setText(pay_card_no.substring(0, 4)+"    "+"***"+"    "+"***"+"    "+"***"+"    "+pay_card_no.substring(pay_card_no.length()-3,pay_card_no.length()));
        fk_carname_double.setText(paylistdata.name);
        pay_card_no=paylistdata.idcard;
        fk_idcarno_double.setText(pay_card_no.substring(0, 4)+"    "+"***"+"    "+"***"+"    "+"***"+"    "+pay_card_no.substring(pay_card_no.length()-3,pay_card_no.length()));
        money_double.setText(money+"");
        phone_double.setText(paylistdata.phone);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_double_iv:
                finish();
                break;
            case R.id.czhi_double_bt:
                    yzm=yzm_double_et.getText().toString().trim();
                    if (TextUtils.isEmpty(yzm)){
                        tishi("请输入验证码");
                    }else {
                        dialog=Util.createLoadingDialog(RbqdzfActivity.this);
                        rbZfQR();
                    }
                break;
            default:
                break;
        }
    }
    //荣邦积分确认支付
    private void  rbZfQR() {
        AsyncHttpClient ahc = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        params.put("user_id",user_id);
        params.put("way",way_id);
        params.put("ordercode",orderno); // 订单唯一码，预支付返回的
        params.put("authcode",yzm); //短信验证码）
        Log.e("荣邦积分确认支付url",Constant.RBZFQR_JF_URL);
        Log.e("荣邦积分确认支付params",params.toString());
        //url:   parmas：请求时携带的参数信息   responseHandler：是一个匿名内部类接受成功过失败
        ahc.post(Constant.RBZFQR_JF_URL, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                //判断状态码
                if(i == 200){
                    //获取结果
                    try {
                        String result = new String(bytes, "utf-8");
                        //                        Toast.makeText(RbjfSelectActivity.this, result, Toast.LENGTH_SHORT).show();
                        Log.e("荣邦积分确认支付Result", result);
                        try {
                            JSONObject jsonObject = new JSONObject(result);
                            Util.closeDialog(dialog);
                            String info = jsonObject.getString("info");
                            if (jsonObject.get("flag").equals("success")) {
                                if (jsonObject.has("data")){
                                    String ret=jsonObject.getJSONObject("data").getJSONObject("data").getString("ret");
                                    info=jsonObject.getJSONObject("data").getJSONObject("data").getString("message");
                                    if (ret.equals("0")){
                                        Intent intent = new Intent(RbqdzfActivity.this,
                                                MainActivity.class);
                                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                        startActivity(intent);
                                        Toast.makeText(RbqdzfActivity.this,"支付成功",Toast.LENGTH_SHORT).show();
                                        finish();
                                    }else{
                                        tishi(info);
                                    }
                                }
                            } else {
                                Util.closeDialog(dialog);
                                if (jsonObject.has("data")){
                                    info=jsonObject.getJSONObject("data").getJSONObject("data").getString("dealmsg");
                                }
                                tishi(info);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    } catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                Util.closeDialog(dialog);
            }
        });
    }
}
